home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
FM Towns: Free Software Collection 6
/
FM Towns Free Software Collection 6.iso
/
t_os
/
book
/
src
/
mk.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-07-08
|
3KB
|
132 lines
#include <stdio.h>
#include <stdefs.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <sys\stat.h>
#define ERROR (-1)
#define BUF_SIZE 32768
static char outbuf[BUF_SIZE] ;
static size_t outsize = 0 ;
char *comma( long val )
{
static char buf[32] ;
char str[32] ;
char *p, *q ;
int i ;
ltoa( val, str, 10 ) ;
for( p=str, q=buf, i=strlen(str) ; i > 0 ; i-- )
{
*q++ = *p++ ;
if( i > 1 && ( (i-1) % 3 ) == 0 )
*q++ = ',' ;
}
*q = '\0' ;
return buf ;
}
int flush( int fd )
{
return write( fd, outbuf, outsize ) ;
}
/* ファイル出力 */
long output( const char *fname, long maxline, short width )
{
static char buf[81] ;
auto char tmp[64], *p ;
auto short max, len ;
auto short fd ;
auto long fsize ;
/* ファイル作成 */
if( ( fd = creat( fname, S_IREAD|S_IWRITE ) ) == ERROR )
{
printf( "can't make output file <%s>\n", fname ) ;
exit( 1 ) ;
}
/* バッファセット */
memset( buf, 0x20, 255 ) ;
buf[width-2] = '\0' ;
strcat( buf, "\x0D\x0A" );
/* 出力 */
outsize = 0 ;
len = strlen( strcpy( tmp, "\x0D\x1BT 残り " ) ) ;
p = tmp + len ;
while( maxline > 0 )
{
strcpy( p, comma( maxline ) ) ;
strcat( p, "line" ) ;
write( 1, tmp, len + strlen( p ) ) ;
if( maxline < 1000 ) max = maxline ;
else max = 1000 ;
maxline -= max ;
while( max-- > 0 )
{
if( outsize + width > BUF_SIZE )
{
if( flush( fd ) < outsize )
{
maxline = 0 ;
break ;
}
outsize = 0 ;
}
memcpy( outbuf+outsize, buf, width ) ;
outsize += width ;
}
}
flush( fd ) ;
fsize = filelength( fd ) ;
close( fd ) ;
return fsize ;
}
void main( int argc, char *argv[] )
{
long maxline ;
short width = 0 ;
const char *out_file_name = "tmp.aaa" ;
const char *help = "mk {行数} [文字数] --- "
"指定文字数×行数のファイルを作ります\n" ;
/* パラメータ処理 */
if( argc > 1 && isdigit( *argv[1] ) )
{
maxline = atol( argv[1] ) ;
if( argc == 3 )
width = atoi( argv[2] ) ;
}
else
{
puts( help ) ;
return ;
}
if( width > 78 ) width = 78 ;
if( width < 2 ) width = 2 ;
printf( "\n %dbyte × %sline = ",
width, comma( maxline ) ) ;
printf( "%sbytes\n\n", comma( width*maxline ) ) ;
/* ファイルの作成 */
printf( "\x0D\x1BT %s の作成完了 ... サイズ %sbytes\n", out_file_name,
comma( output( out_file_name, maxline, width ) ) ) ;
}